package view;

import Dao.EmpDao;
import bean.Emp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import service.EmpService;
import service.Impl.EmpServiceImpl;
import util.CMUtility;
import util.DBUtils;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class EmpMain {
    static Scanner sc = new Scanner(System.in);
    static EmpService empService = new EmpServiceImpl();

    /**
     * 程序入口
     */

    public static void main(String[] args) {

        System.out.println("\t\t\t\t******************************");
        System.out.println("\t\t\t\t**********员工管理系统**********");
        System.out.println("\t\t\t\t******************************");
        meaus();

    }

    private static void meaus() {
        boolean flag = true;
        while (flag) {
            System.out.println("请输入选项，1、员工列表    2、添加员工  3、修改员工资料    4、删除员工  5、模糊查询  6、退出");
            System.out.print("请输入入1-6中的任意字符：");

            char c = CMUtility.readMenuSelection();
            switch (c) {
                case '1':
                    List<Emp> empList = empService.getAllEmp();
                    for (Emp emp : empList) {
                        System.out.println(emp);
                    }
                    ;
                    System.out.println("--------------查询完毕----------------");
                    ;
                    break;

                case '2':
                    Emp emp = new Emp();
                    Date hiredate;
                    Scanner scan = new Scanner(System.in);
                    System.out.print("请输入工号：");
                    //emp.setEmpno(sc.nextInt());
                    emp.setEmpno(CMUtility.readInt());
                    System.out.print("请输入姓名：");
                    emp.setEname(sc.next());
                    System.out.print("请输入工作：");
                    emp.setJob(sc.next());
                    System.out.print("请输入收入：");
                    emp.setSal(sc.nextDouble());
                    System.out.print("请输入员工入职时间,格式为（yyyy-MM-dd）:");
                    for (; ; ) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String dateStr = scan.next();
                        try {
                            hiredate = (Date) format.parse(dateStr);
                            java.sql.Date hire = new java.sql.Date(hiredate.getTime());
                            emp.setHiredate(hire);

                            break;
                        } catch (ParseException e) {
                            System.out.print("格式错误，请重新输入：");
                        }
                    }
                    System.out.print("请输入奖金：");
                    emp.setComm(sc.nextDouble());
                    System.out.print("请输入部门号：");
                    emp.setDeptno(sc.nextInt());
                    empService.addEmp(emp);
                    break;
                case '3':
                    System.out.print("请输入员工号(长度不能超过四位)：");
                    int empn = sc.nextInt();
                    empService.upData(empn);
                    break;

                case '4':
                    System.out.print("请输入要删除的成员的工号(长度不能超过四位)：");
                    int empno = CMUtility.readInt();
                    //int empno = sc.nextInt();
                    int delete = empService.delEmp(empno);
                    if (delete == 1) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    break;
                case '5':
                    System.out.print("请输入员工姓名中的任意一个字");
                    String name = sc.next();
                    List<Emp> list = empService.selectEmp(name);
                    for (Emp p : list) {
                        System.out.println(p);
                    }
                    break;
                case '6':
                    System.out.print("确定是否退出（Y/N）:");
                    char exit = CMUtility.readConfirmSelection();
                    if (exit == 'Y') {
                        System.out.println("退出系统！");
                        flag = false;
                    }


            }
        }
    }
}
